package com.amigo.online.provider.manager.util;

import java.util.Calendar;
import java.util.TimeZone;

import com.amigo.online.provider.manager.util.param.dto.PageDto;

public class TimestampUtil {

    /**
     * 上个月第一天时间戳
     *
     * @rethisrn
     */
    public static String firstDayOfPrecedingMonth() {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, -1);
        c.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天
        // 将小时至0
        c.set(Calendar.HOUR_OF_DAY, 0);
        // 将分钟至0
        c.set(Calendar.MINUTE, 0);
        // 将秒至0
        c.set(Calendar.SECOND, 0);
        // 将毫秒至0
        c.set(Calendar.MILLISECOND, 0);

        return String.valueOf(c.getTimeInMillis()).substring(0, 10);

    }

    /**
     * 上个月最后一天的时间戳
     *
     * @rethisrn
     */
    public static String lastDayOfPrecedingMonth() {
        // 获取当前月最后一天
        Calendar c = Calendar.getInstance();
        c.set(Calendar.DAY_OF_MONTH, 0);
        // 将小时至0
        c.set(Calendar.HOUR_OF_DAY, 23);
        // 将分钟至0
        c.set(Calendar.MINUTE, 59);
        // 将秒至0
        c.set(Calendar.SECOND, 59);
        // 将毫秒至0
        c.set(Calendar.MILLISECOND, 59);

        return String.valueOf(c.getTimeInMillis()).substring(0, 10);

    }

    /**
     * 昨天凌晨
     *
     * @rethisrn
     */
    public static String firstOfYesterday() {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DATE, -1);
        // 将小时至0
        c.set(Calendar.HOUR_OF_DAY, 0);
        // 将分钟至0
        c.set(Calendar.MINUTE, 0);
        // 将秒至0
        c.set(Calendar.SECOND, 0);
        // 将毫秒至0
        c.set(Calendar.MILLISECOND, 0);

        return String.valueOf(c.getTimeInMillis()).substring(0, 10);

    }

    /**
     * 昨天晚上
     *
     * @rethisrn
     */
    public static String lastOfYesterday() {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DATE, -1);
        // 将小时至0
        c.set(Calendar.HOUR_OF_DAY, 23);
        // 将分钟至0
        c.set(Calendar.MINUTE, 59);
        // 将秒至0
        c.set(Calendar.SECOND, 59);
        // 将毫秒至0
        c.set(Calendar.MILLISECOND, 59);

        return String.valueOf(c.getTimeInMillis()).substring(0, 10);

    }

    /**
     * 前天凌晨
     *
     * @rethisrn
     */
    public static String firstOfBeforeerday() {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DATE, -2);
        // 将小时至0
        c.set(Calendar.HOUR_OF_DAY, 0);
        // 将分钟至0
        c.set(Calendar.MINUTE, 0);
        // 将秒至0
        c.set(Calendar.SECOND, 0);
        // 将毫秒至0
        c.set(Calendar.MILLISECOND, 0);

        return String.valueOf(c.getTimeInMillis()).substring(0, 10);

    }

    /**
     * 前天晚上
     *
     * @rethisrn
     */
    public static String lastOfBeforeerday() {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DATE, -2);
        // 将小时至0
        c.set(Calendar.HOUR_OF_DAY, 23);
        // 将分钟至0
        c.set(Calendar.MINUTE, 59);
        // 将秒至0
        c.set(Calendar.SECOND, 59);
        // 将毫秒至0
        c.set(Calendar.MILLISECOND, 59);

        return String.valueOf(c.getTimeInMillis()).substring(0, 10);

    }

    /**
     * 上周第一天 周日
     *
     * @rethisrn
     */
    public static String lastSunday() {
        Calendar c = Calendar.getInstance();
        int dayOfWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
        int offset1 = 1 - dayOfWeek;
        c.add(Calendar.DATE, offset1 - 8);
        // 将小时至0
        c.set(Calendar.HOUR_OF_DAY, 0);
        // 将分钟至0
        c.set(Calendar.MINUTE, 0);
        // 将秒至0
        c.set(Calendar.SECOND, 0);
        // 将毫秒至0
        c.set(Calendar.MILLISECOND, 0);

        return String.valueOf(c.getTimeInMillis()).substring(0, 10);
    }

    /**
     * 上周最后一天
     *
     * @rethisrn
     */
    public static String lastSaturday() {
        Calendar c = Calendar.getInstance();
        int dayOfWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
        int offset2 = 7 - dayOfWeek;
        c.add(Calendar.DATE, offset2 - 8);
        // 将小时至0
        c.set(Calendar.HOUR_OF_DAY, 23);
        // 将分钟至0
        c.set(Calendar.MINUTE, 59);
        // 将秒至0
        c.set(Calendar.SECOND, 59);
        // 将毫秒至0
        c.set(Calendar.MILLISECOND, 59);

        return String.valueOf(c.getTimeInMillis()).substring(0, 10);
    }

    /**
     * 3个月内
     *
     * @rethisrn
     */
    public static String withinThreeMonths() {

        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, -3);
        c.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天
        // 将小时至0
        c.set(Calendar.HOUR_OF_DAY, 0);
        // 将分钟至0
        c.set(Calendar.MINUTE, 0);
        // 将秒至0
        c.set(Calendar.SECOND, 0);
        // 将毫秒至0
        c.set(Calendar.MILLISECOND, 0);
        return String.valueOf(c.getTimeInMillis()).substring(0, 10);
    }


    public static void setQueryTime(PageDto dto) {
        Integer ii = dto.getTimetype();
        // ii: 1.昨天; 2.前天;3.上周;4.上月;5.3个月内
        if (ii != null) {
            switch (ii) {
                case 1:
                    dto.setFirstTime(TimestampUtil.firstOfYesterday());
                    dto.setLastTime(TimestampUtil.lastOfYesterday());
                    break;
                case 2:
                    dto.setFirstTime(TimestampUtil.firstOfBeforeerday());
                    dto.setLastTime(TimestampUtil.lastOfBeforeerday());
                    break;
                case 3:
                    dto.setFirstTime(TimestampUtil.lastSunday());
                    dto.setLastTime(TimestampUtil.lastSaturday());
                    break;
                case 4:
                    dto.setFirstTime(TimestampUtil.firstDayOfPrecedingMonth());
                    dto.setLastTime(TimestampUtil.lastDayOfPrecedingMonth());
                    break;
                case 5:
                    dto.setFirstTime(TimestampUtil.withinThreeMonths());
                    break;
                default:
                    break;
            }

        }

    }


    /**
     * 生成毫秒时间戳加6位随机数字符串
     *
     * @return
     */
    public static String timestampRandomNumbers() {
        long tt = System.currentTimeMillis();
        long mr = (int) ((Math.random() * 9 + 1) * 100000);

        return (String.valueOf(tt) + String.valueOf(mr));


    }

    public static void main(String[] args) {

        System.out.println(TimestampUtil.timestampRandomNumbers());
    }


    public static Long getNowDay0Time() {
        long current = System.currentTimeMillis();//当前时间毫秒数
        long zero = current / (1000 * 3600 * 24) * (1000 * 3600 * 24) - TimeZone.getDefault().getRawOffset();//今天零点零分零秒的毫秒数
        return zero / 1000;
    }


}
